package net.imagine.frontend.skyline;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Component("uploadAction")
public class UploadAction {
//	private static final Log logger = LogFactory.getLog(UploadAction.class);
//	private ObjectMapper mapper = new ObjectMapper();
//	@Value("${imagine.localStorePath}")
//	private String basePath;
//	private ImageOperations operations;
//	@Value("${imagine.localStorePath}")
//	private String batchUpUrl;
	private static final String URL_SUFFIX = "html";
	
	
	public void batchUpload(HttpServletRequest request, HttpServletResponse response) throws IOException {
		if(!"POST".equals(request.getMethod())){
			String albumId = request.getParameter("albumid");
			response.sendRedirect(buildRecirectPath(request, "/album/bupload/"+albumId));
		}
		List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
		// Only One File In SwfUpload Per Request
		MultipartFile file = files == null ? null : files.get(0);
		if(file != null) {
			System.out.println(file.getName());
			return;
		}
//		op
	}

	protected String buildRecirectPath(HttpServletRequest request, String baseUrl) {
		return request.getContextPath() + baseUrl + URL_SUFFIX;
	}

	
	
	
	
	
	
	
	
	
	
//	@SuppressWarnings("unchecked")
//	public void batchUpload(HttpServletRequest request, HttpServletResponse response) throws IOException {
//		
//		List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
//		// Only One File In SwfUpload Per Request
//		MultipartFile file = files == null ? null : files.get(0);
//		String albumId = request.getParameter("albumid");
//		// Long userId = 1L;
//		StringBuffer msg = new StringBuffer();
//		File localFile = storeFileInLocal(Long.parseLong(albumId), file, msg);
//		String json = null;
//		if (localFile == null) {
//			json = toJsonString(false, "9", msg.toString(), null);
//		} else {
//			MultiValueMap<String, String> localFiles = (MultiValueMap<String, String>) request.getSession()
//					.getAttribute("files");
//			if (localFiles == null) {
//				localFiles = new LinkedMultiValueMap<String, String>();
//			}
//			localFiles.add(albumId, localFile.getAbsolutePath());
//			request.getSession().setAttribute("files", localFiles);
//			json = toJsonString(true, "", msg.toString(), null);
//		}
//
//		PrintWriter out = response.getWriter();
//		out.print(json);
//	}

//	private String toJsonString(boolean isSuccess, String errorCode, String errorMsg, Map<String, Object> srcMsg)
//			throws JsonGenerationException, JsonMappingException, IOException {
//		if (srcMsg == null) {
//			srcMsg = new HashMap<String, Object>();
//		}
//		if (isSuccess) {
//			srcMsg.remove("error");
//		} else {
//			srcMsg.put("error", errorCode);
//		}
//		srcMsg.put("msg", errorMsg);
//		return mapper.writeValueAsString(srcMsg);
//	}
//
//	private File storeFileInLocal(Long albumId, MultipartFile file, StringBuffer msg) throws IOException {
////		String basePath = servletCtxResource.getFile().getAbsolutePath();
//		String oriFilename = file.getOriginalFilename();
//		String ext = FilenameUtils.getExtension(oriFilename);
//		String filename = DigestUtils.md5Hex(albumId + oriFilename);
//		File localFile = new File(basePath, (filename + "." + ext));
//		int num = 0;
//		while (localFile.exists()) {// 处理同名文件
//			StringBuffer tempFilename = new StringBuffer(filename);
//			tempFilename.append("(").append(num).append(")").append(".").append(ext);
//			localFile = new File(basePath, tempFilename.toString());
//			num++;
//		}
//		FileOutputStream out = FileUtils.openOutputStream(localFile);
//		try {
//			IOUtils.copy(file.getInputStream(), out);
//		} catch (IOException e) {
//			logger.warn("写入本地临时文件失败", e);
//			msg.append("上传失败");
//			return null;
//		}
//		IOUtils.closeQuietly(out);
//		return localFile;
//	}
}
